package pers.cz.logtail.config;

import pers.cz.exception.ErrorCode;
import pers.cz.exception.LogSaveException;
import pers.cz.exception.RegisterException;
import pers.cz.logtail.regedit.ServiceRegedit;

public enum RegeditTypeEnum {

    ;
    /**
     * 持久化方案
     */
    String serviceRegeditName;

    Class<? extends ServiceRegedit> serviceRegeditClass;

    RegeditTypeEnum(String serviceRegeditName, Class<? extends ServiceRegedit> serviceRegeditClass) {
        this.serviceRegeditName = serviceRegeditName;
        this.serviceRegeditClass = serviceRegeditClass;
    }

    public String getServiceRegeditName() {
        return serviceRegeditName;
    }

    public Class<? extends ServiceRegedit> getServiceRegeditClass() {
        return serviceRegeditClass;
    }

    public static Class<? extends ServiceRegedit> getServiceRegeditClassByRegeditName(String savePlanName) {

        RegeditTypeEnum nodeTypeEnum = getEnumByServiceRegeditName(savePlanName);

        if (nodeTypeEnum == null) {
            throw new RegisterException(ErrorCode.GET_REGISTER_IMPL_ERROR, savePlanName);
        }
        return nodeTypeEnum.getServiceRegeditClass();
    }

    public static RegeditTypeEnum getEnumByServiceRegeditName(String savePlanName) {
        for (RegeditTypeEnum anEnum : values()) {
            if (anEnum.getServiceRegeditName().equalsIgnoreCase(savePlanName)) {
                return anEnum;
            }
        }
        return null;
    }

    public static ServiceRegedit getServiceRegeditInstance(String savePlanName) {

        Class<? extends ServiceRegedit> nodeClassByNodeName = getServiceRegeditClassByRegeditName(savePlanName);
        ServiceRegedit logSave = null;
        try {
            logSave = nodeClassByNodeName.getConstructor().newInstance();
        } catch (Exception e) {
            throw new LogSaveException(ErrorCode.SAVE_PLAN_IMPL_IS_NULL_ERROR, savePlanName);
        }

        return logSave;
    }

}
